package com.budgetbears.champagneshaketitle;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.animator.AlphaMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;

public class ChampagneShakeTitle extends BaseGameActivity implements IOnAreaTouchListener, IOnMenuItemClickListener {
	
	// ===========================================================
	// Constants
	// ===========================================================
	
	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	
	protected static final int MENU_NEW = 0;
    protected static final int MENU_SCORE = MENU_NEW + 1;
    //Need a single instance of this across both classes!
    protected static ScoreDatabase database;

	// ===========================================================
	// Fields
	// ===========================================================
    
    private Camera mCamera;
    private Sprite backgroundSprite;
    private Texture mBackgroundTexture;
	private Texture mTitleMenuTexture;
	private TextureRegion mTitleNewRegion;
	private TextureRegion mTitleScoreRegion;
	private TextureRegion mTitleBackgroundRegion;
	
	//Scenes
	protected Scene backgroundScene;
	protected Scene scoreScene;
	protected MenuScene mTitleScene;

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		//Create textures for main menu
		this.mBackgroundTexture = new Texture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mTitleMenuTexture = new Texture(512, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mTitleBackgroundRegion = TextureRegionFactory.createFromAsset(this.mBackgroundTexture, this, "gfx/champ_menu.png", 0, 0);
        this.mTitleNewRegion = TextureRegionFactory.createFromAsset(this.mTitleMenuTexture, this, "gfx/play_button.png", 163, 0);
        this.mTitleScoreRegion = TextureRegionFactory.createFromAsset(this.mTitleMenuTexture, this, "gfx/hi_score_button.png", 49, 70);
        this.mEngine.getTextureManager().loadTexture(this.mBackgroundTexture);
        this.mEngine.getTextureManager().loadTexture(this.mTitleMenuTexture);
		
	}

	@Override
	public Scene onLoadScene() {
		//Create sprite for title background and create title scene - calling the method, set the background for the scene
		backgroundSprite = new Sprite(0, 0, mTitleBackgroundRegion);
		//set the size of the background sprite to be the same as the camera size
		backgroundSprite.setSize(CAMERA_WIDTH, CAMERA_HEIGHT);
		backgroundScene = new Scene(1);
		backgroundScene.setBackground(new SpriteBackground(backgroundSprite));
		
		this.createTitleScene();
		backgroundScene.setChildScene(mTitleScene);
        
        return this.backgroundScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
		case MENU_NEW:
			//Load game Intent
			Intent intent = new Intent(this, ChampagneShakeGame.class);
			this.startActivity(intent);
			return true;
		case MENU_SCORE:
				//TODO - Temporarily quits the game until high scores are implemented
			Intent intent2 = new Intent(this, ScoreTable.class);
			this.startActivity(intent2);
				return true;
        default:
                return false;
		}
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		return false;
	}
	
	//Method to create the menu scene
    protected void createTitleScene() {
        this.mTitleScene = new MenuScene(this.mCamera);

        final SpriteMenuItem newMenuItem = new SpriteMenuItem(MENU_NEW, this.mTitleNewRegion);
        newMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mTitleScene.addMenuItem(newMenuItem);
        
        final SpriteMenuItem scoreMenuItem = new SpriteMenuItem(MENU_SCORE, this.mTitleScoreRegion);
        scoreMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mTitleScene.addMenuItem(scoreMenuItem);
        
        this.mTitleScene.setMenuAnimator(new AlphaMenuAnimator(20));
        
        this.mTitleScene.buildAnimations();

        this.mTitleScene.setBackgroundEnabled(false);

        this.mTitleScene.setOnMenuItemClickListener(this);
    }
    
    //Create scene for scores
    public void createScoreScene() {
    	scoreScene = new Scene(1);
    	
    }
/*    
  //Method to get high scores from database
    public void GetDatabaseScores() {
    	database = new ScoreDatabase(ChampagneShakeTitle.this);
		SQLiteDatabase db = database.getWritableDatabase();
		//TODO - Do Stuff
    }
*/
}